package com.enjoyor.soa.traffic.rest.tsms.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.enjoyor.soa.traffic.api.dto.tsms.ExecuteTaskDispatchDto;
import com.enjoyor.soa.traffic.api.dto.tsms.RltRoutePostDto;
import com.enjoyor.soa.traffic.frame.annotation.Auth;
import com.enjoyor.soa.traffic.frame.security.bean.dto.UserDto;
import com.enjoyor.soa.traffic.rest.tsms.dubbo.IExecuteTaskDispatchConsumeService;
import com.enjoyor.soa.traffic.rest.tsms.dubbo.invoke.UbmsInfoServiceInvoke;
import com.enjoyor.soa.traffic.util.helper.ResultHelper;
import com.enjoyor.soa.traffic.util.pojo.Page;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;
import com.enjoyor.soa.traffic.util.pojo.TreeNode;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;

@Auth
@RestController
@RequestMapping({ "/ExecuteTaskDispatch" })
@Api("ExecuteTaskDispatchController相关api")
@CrossOrigin
public class ExecuteTaskDispatchController {

	@Autowired
	private IExecuteTaskDispatchConsumeService iConsumeExecuteTaskDispatchService;
	
	@Autowired
	private UbmsInfoServiceInvoke ubmsInfoServiceInvoke;

	@Value("${root.dept}")
    private String rootDeptId;
	
	/**
	 * 派警分页信息；
	 * 
	 * @return zyh
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@GetMapping(value = { "/getExecuteTaskDispatchPage" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("派警分页信息")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "dispatchId", value = "派警编号", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "executeTaskId", value = "执行任务编号", paramType = "query", dataType = "string") })
	public ResultPojo getExecuteTaskDispatchPage(
			@RequestParam(defaultValue = "") String dispatchId,
			@RequestParam(defaultValue = "") String executeTaskId,
			Page page, HttpServletRequest request) {
		ResultPojo resultPojo = new ResultPojo();
		resultPojo = iConsumeExecuteTaskDispatchService.getExecuteTaskDispatchPage(page,dispatchId, executeTaskId);
		return resultPojo;
	}

	/**
	 * 派警list；
	 * 
	 * @return zyh
	 */
	@GetMapping(value = { "/getExecuteTaskDispatchList" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiResponse(response = ExecuteTaskDispatchDto.class, code = 0, message = "")
	@ApiOperation("派警信息List查询")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "dispatchId", value = "派警编号", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "executeTaskId", value = "执行任务编号", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "postId", value = "岗位编号", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "policeId", value = "警员编号", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "policeName", value = "警员名称", paramType = "query", dataType = "string") })
	public ResultPojo getExecuteTaskDispatchList(
			@RequestParam(defaultValue = "") String executeTaskId,
			@RequestParam(defaultValue = "") String dispatchId,
			@RequestParam(defaultValue = "") String postId,
			@RequestParam(defaultValue = "") String policeId,
			@RequestParam(defaultValue = "") String policeName,
			HttpServletRequest request) {
		return iConsumeExecuteTaskDispatchService.getExecuteTaskDispatchList(dispatchId, executeTaskId, postId,
				policeId, policeName);
	}

	/**
	 * 获取岗位及对应派警列表信息
	 * 
	 * @return fzj
	 */
	@GetMapping(value = { "/getPostPoliceDispatchList" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("获取岗位及对应派警列表信息")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "taskId", value = "任务编号", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "executeTaskId", value = "执行任务编号", paramType = "query", dataType = "string"),
		@ApiImplicitParam(name = "routeId", value = "线路编号", paramType = "query", dataType = "string")})
	public ResultPojo getPostPoliceDispatchList(
			@RequestParam(defaultValue = "", value="taskId", required=true) String taskId,
			@RequestParam(defaultValue = "", value="executeTaskId", required=true) String executeTaskId,
			@RequestParam(defaultValue = "", value="routeId", required=true) String routeId,
			HttpServletRequest request) {
		return iConsumeExecuteTaskDispatchService.getPostPoliceDispatchList(taskId, executeTaskId, routeId);
	}
	
	/**
	 * 添加派警信息；
	 * 
	 * @return zyh
	 */
	@PostMapping(value = { "/addExecuteTaskDispatch" }, consumes = { "application/json; charset=UTF-8" }, produces = {
			"application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("添加岗位信息")
	public ResultPojo addExecuteTaskDispatch(
			@RequestBody ExecuteTaskDispatchDto executeTaskDispatchDto, HttpServletRequest request) {
		return iConsumeExecuteTaskDispatchService.addExecuteTaskDispatch(executeTaskDispatchDto,
				((UserDto) request.getAttribute("currentUser")).getPersonName());
	}

	/**
	 * 修改单条派警信息；
	 * 
	 * @return zyh
	 */
	@PutMapping(value = { "/editExecuteTaskDispatch" }, consumes = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("修改派警信息")
	public ResultPojo editExecuteTaskDispatch(
			@RequestBody ExecuteTaskDispatchDto executeTaskDispatchDto, HttpServletRequest request) {
		return iConsumeExecuteTaskDispatchService.editExecuteTaskDispatch(executeTaskDispatchDto,
				((UserDto) request.getAttribute("currentUser")).getPersonName());
	}

	/**
	 * 删除派警信息；
	 * 
	 * @return zyh
	 */
	@DeleteMapping(value = { "/delExecuteTaskDispatch" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("删除派警信息")
	@ApiImplicitParam(name = "dispatchId", value = "派警编号", paramType = "query", required = true, dataType = "String")
	public ResultPojo delRltRoutePost(
			@RequestParam(defaultValue = "",value="dispatchId", required=true) String dispatchId,
			HttpServletRequest request) {
		return iConsumeExecuteTaskDispatchService.deleteExecuteTaskDispatch(dispatchId,
				((UserDto) request.getAttribute("currentUser")).getPersonName());
	}

	/**
	 * 派警
	 * @return zyh
	 */
	@PutMapping(value = { "/addDispatchPolice" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("派警")
	public ResultPojo addDispatchPolice(@RequestParam("executeTaskId") String executeTaskId,
			@RequestBody List<ExecuteTaskDispatchDto> ExecuteTaskDispatList, HttpServletRequest request) {
		return iConsumeExecuteTaskDispatchService.assignExecuteTaskDispatch(ExecuteTaskDispatList, executeTaskId,
				((UserDto) request.getAttribute("currentUser")).getPersonName());
	}
	
	/**
	 * 获取警员基础信息列表；
	 * 
	 * @return fzj
	 */
	@GetMapping(value = { "/getPersonInfo" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("警员基础信息List查询")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "deptId", value = "部门编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "personKey", value = "关键字（警员编号/警员名称）", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "showNum", value = "显示数据条数", paramType = "query", dataType = "String") })
	public ResultPojo getPersonInfo(
			@RequestParam(defaultValue = "", value="deptId", required=false) String deptId,
			@RequestParam(defaultValue = "", value="personKey", required=false) String personKey,
			@RequestParam(defaultValue = "", value="showNum", required=false) String showNum,
			HttpServletRequest request) {
		/*UserDto userDto = (UserDto) request.getAttribute("currentUser");
		String orgId = userDto.getOrgId();
		if(null == deptId || "".equals(deptId)) {
			deptId = orgId;
		}*/
		return ubmsInfoServiceInvoke.getPersonInfo(deptId, personKey, showNum);
	}

	/**
	 * 警员基础信息分页查询
	 * 
	 * @return fzj
	 */
	@GetMapping(value = { "/getPersonInfoPage" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("警员基础信息分页查询")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "deptId", value = "部门编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "personKey", value = "关键字（警员编号/警员名称）", paramType = "query", dataType = "String") })
	public ResultPojo getPersonInfoPage(
			@RequestParam(defaultValue = "", value="deptId", required=false) String deptId,
			@RequestParam(defaultValue = "", value="personKey", required=false) String personKey,
			Page page, HttpServletRequest request) {
		ResultPojo resultPojo = new ResultPojo();
		/*UserDto userDto = (UserDto) request.getAttribute("currentUser");
		String orgId = userDto.getOrgId();*/
		if(null == deptId || "".equals(deptId)) {
			deptId = rootDeptId;
		}
		resultPojo = ubmsInfoServiceInvoke.getPersonInfoPage(deptId, personKey, page);
		return resultPojo;
	}
	
	/**
	 * 获取警员列表信息
	 * @return fzj
	 */
	@GetMapping(value = { "/getPersonInfoList" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("获取警员列表信息")
	@ApiImplicitParams({ 
		@ApiImplicitParam(name = "deptId", value = "部门编号", paramType = "query", dataType = "String"),
		@ApiImplicitParam(name = "personKey", value = "关键字（警员编号/警员名称）", paramType = "query", dataType = "String") })
	public ResultPojo getPersonInfoList(
			@RequestParam(defaultValue = "", value="deptId", required=false) String deptId,
			@RequestParam(defaultValue = "", value="personKey", required=false) String personKey, 
			HttpServletRequest request) {
		ResultPojo resultPojo = new ResultPojo();
		if(null == deptId || "".equals(deptId)) {
			deptId = rootDeptId;
		}
		resultPojo = ubmsInfoServiceInvoke.getPersonInfoList(deptId, personKey);
		return resultPojo;
	}
	
	
	/**
	 * 获取部门信息树；
	 * 
	 * @return fzj
	 */
	@GetMapping(value = { "/getDeptTree" }, produces = { "application/json; charset=UTF-8" })
	@ResponseBody
	@ApiOperation("获取部门信息树")
	@ApiImplicitParam(name = "deptId", value = "部门编号", paramType = "query", dataType = "String")
	public ResultPojo getDeptTree(
			@RequestParam(defaultValue = "", value="deptId", required=false) String deptId,
			HttpServletRequest request) {
		UserDto userDto = (UserDto) request.getAttribute("currentUser");
		String token = (String)request.getAttribute("token");
		String orgId = userDto.getOrgId();
		if(null == deptId || "".equals(deptId)) {
			deptId = orgId;
		}
		List<TreeNode> treeNodeList = ubmsInfoServiceInvoke.getDeptTree(token, deptId);
		return ResultHelper.getResult(treeNodeList);
	}
}
